
# 无用文件

import os, sys, subprocess



def process(i, o):
    if os.path.exists(o):
        os.remove(o)
    cmd="ffmpeg -i {} -vcodec copy -acodec copy -vbsf h264_mp4toannexb  {} ".format(i, o)  #  -bsf:v h264_mp4toannexb   -f mpegts
    os.system(cmd)

def batch_process(in_dir, out_dir):
    fns = os.listdir(in_dir)
    ps = [ os.path.join(in_dir, fn)  for fn in fns  ]
    ops = [ os.path.join(out_dir, fn )  for fn in fns  ]

    i = 0
    for i in range( len(ps) ):
        process( ps[i],  ops[i] )
    print("end")




def gen_txt(video_dir:str):
    fns = os.listdir(in_dir)
    ps = [ os.path.join(in_dir, fn)  for fn in fns  ]
    with open("files.txt", 'w', encoding="utf-8") as f:
        for p in ps:
            f.write("file '{}'\n".format(p) )

# 

def merge_by_txt(out_mp4):
    cmd= 'ffmpeg -f concat -safe 0 -i "files.txt" -c copy "{}"'.format(out_mp4)
    print("ret=", os.system(cmd))

if __name__ == "__main__":
    in_dir = "工作区1/视频"
    out_dir = "工作区1/processed"
    if not os.path.exists(out_dir):
        os.mkdir(out_dir)
    #预处理
    # batch_process(in_dir, out_dir) 

    # #合并
    # gen_txt(out_dir)
    # merge_by_txt('工作区1/合并.mp4')
    pass








